package com.education.service.impl;

import java.util.Date;
import java.util.List;

import com.education.model.StudyResModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.education.dao.StudyResourceDao;
import com.education.model.ResourceTCModel;
import com.education.model.StudyResourceModel;
import com.education.service.IStudyResService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 导学资源服务层服务
 *
 * @author 申忠正
 */
@Service
public class StudyResServiceImpl implements IStudyResService {
    /**
     * 注入导学资源的数据层接口 StudyResourceDao
     */
    @Autowired
    private StudyResourceDao studyResourceDao;

    /**
     * @param pageNo   当前页
     * @param pageSize 总数据条数
     * @return PageInfo<StudyResourceModel> 分页实体 【里面存放的是导学资源List】
     * @throws Exception 业务处理层异常
     */
    public PageInfo<ResourceTCModel> selectStudyResource(ResourceTCModel resourceTCModel, Integer pageNo, Integer pageSize) throws Exception {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 3 : pageSize;
        //开启分页 参数为当前页和数据条数
        PageHelper.startPage(pageNo, pageSize);
        //查询结果返回
        List<ResourceTCModel> list = studyResourceDao.selectStudyResource(resourceTCModel);
        //将查询结果传给PageInfo对象
        PageInfo<ResourceTCModel> page = new PageInfo<ResourceTCModel>(list);
        return page;
    }

    @Override
    public int addStudyResource(StudyResModel sr) throws Exception {
        int result = studyResourceDao.addStudyResource(sr);
        return result;
    }

    /**
     * 删除
     * @param resourceId
     * @return
     * @throws Exception
     */
    public int deleteResource(int resourceId) throws Exception {
        int result = studyResourceDao.deleteResource(resourceId);
        return result;
    }

    /**
     * 更新操作第一步 根据id查询
     *
     * @param resourceId 传入资源id
     * @return 返回一个 资源对象
     * @throws Exception
     */
    public StudyResModel queryResById(int resourceId) throws Exception {
        StudyResModel studyResModel = studyResourceDao.queryResById(resourceId);
        return studyResModel;
    }

    /**
     * 更新资料
     *
     * @param studyResModel 实体
     * @return
     * @throws Exception
     */
    @Override
    public int updateResource(StudyResModel studyResModel) throws Exception {
        int result = studyResourceDao.updateResource(studyResModel);
        return result;
    }


    @Override
    public PageInfo<StudyResModel> getStudyResByTeacherId(Integer teacherId, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 15 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<StudyResModel> list = studyResourceDao.getStudyResByTeacherId(teacherId);
        return new PageInfo<>(list);
    }


}
